package com.mj.mafeed.activity;

import com.mj.mafeed.R;
import com.mj.mafeed.service.AreaService;
import com.mj.mafeed.service.CartItemService;
import com.mj.mafeed.service.MobileIndexService;
import com.mj.mafeed.service.PaymentMethodService;
import com.mj.mafeed.service.ProductCategoryService;
import com.mj.mafeed.service.ProductService;
import com.mj.mafeed.service.ShippingMethodService;
import com.mj.mafeed.util.NetUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * 欢迎页
 * @author zhaominglei
 * @date 2015-11-23
 * 
 */
public class SplashActivity extends Activity {
	@SuppressWarnings("unused")
	private static final String TAG = SplashActivity.class.getSimpleName();
	
	private static final int SPLASH_DISPLAY_LENGHT = 20*1000; //延迟20秒
	private static final int STOP_SPLASH = 1;
	private SplashActivityHandler splashActivityHandler = new SplashActivityHandler();
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		
		// 同步数据 产品不多情况下可以同步 多的情况增量拉取
		if (NetUtils.isConnected(getApplicationContext())) {
			new SyncDataTask().start();
		} else {
			splashActivityHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					Intent intent = new Intent(SplashActivity.this, MainActivity.class);
					SplashActivity.this.startActivity(intent);
					SplashActivity.this.finish();
				}
			}, SPLASH_DISPLAY_LENGHT);
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}
	
	// TODO 将来可以加个进度条 后面主程序加载完后通知欢迎页面
	@SuppressLint("HandlerLeak")
	public class SplashActivityHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOP_SPLASH:
				intent = new Intent(SplashActivity.this, MainActivity.class);
				SplashActivity.this.startActivity(intent);
				SplashActivity.this.finish();
				break;
			
			default:
				intent = new Intent(SplashActivity.this, MainActivity.class);
				SplashActivity.this.startActivity(intent);
				SplashActivity.this.finish();
				break;
			}
			super.handleMessage(msg);
		}
	}
	
	/**
	 * 同步数据任务
	 * 
	 */
	public class SyncDataTask extends Thread {
		private MobileIndexService mobileIndexService = new MobileIndexService();
		private ProductCategoryService productCategoryService = new ProductCategoryService();
		private ProductService productService = new ProductService();
		private CartItemService cartItemService = new CartItemService();		
		private AreaService areaService = new AreaService();
		private ShippingMethodService shippingMethodService = new ShippingMethodService();
		private PaymentMethodService paymentMethodService = new PaymentMethodService();

		@Override
		public void run() {
			mobileIndexService.syncMobileIndex(getApplicationContext());//同步手机端首页
			productCategoryService.syncProductCategory(getApplicationContext());//同步商品分类
			productService.syncProduct(getApplicationContext());//同步商品
			cartItemService.syncCartItem(getApplicationContext(), "SplashActivity", null, null);
			
			Message msg = new Message();
			msg.what = STOP_SPLASH;
			splashActivityHandler.sendMessage(msg);
			
			//同步地区
			new Thread(new Runnable() {
				@Override
				public void run() {
					areaService.syncArea(getApplicationContext());
					shippingMethodService.syncShippingMethod(getApplicationContext());
					paymentMethodService.syncPaymentMethod(getApplicationContext());
				}
			}).start();
		}
		
	}
}
